﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XZL.GM.Dal;
using XZL.GM.Entity;


namespace XZL.GM.Bll
{

    public  class UserInfoBiz
    {


        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="type"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        public static List<Proc_UserInfo_SelectListResult> GetUserList(string nickName,string mobilePhone,int userType, int pageIndex, int pageSize, ref int? pageCount)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            return db.Proc_UserInfo_SelectList(nickName,mobilePhone,userType,pageIndex,pageSize,ref pageCount).ToList();
        }


        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns>0：成功  -1：用户名存在    -3：数据库错误</returns>

        public static int UserRegister(string userID, string referrerID,string weiXinID ,string weiXinName, string password ,string mobilePhone)
        {
             XZL_GMDataContext db = new XZL_GMDataContext();

             int i = db.Proc_UserInfo_Register(userID, referrerID, weiXinID, weiXinName, password, mobilePhone);
           
             return i;
        }

        /// <summary>
        /// 后台用户注册
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns>0：成功  -1：用户名存在    -3：数据库错误</returns>
        public static int UserBackReigister(string userID,string userName,string nickName,string mobilePhone,string password,int userType,int status)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            int i = db.Proc_UserInfo_BackRegister(userID, userName, nickName, mobilePhone, password,userType, status);
            return i;
        
        }



        /// <summary>
        /// 更新用户资料
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="realName"></param>
        /// <param name="idCard"></param>
        /// <param name="tel"></param>
        /// <param name="email"></param>
        /// <param name="unc"></param>
        /// <param name="btb"></param>
        /// <returns></returns>
        public static bool UpdateUserInfo(string userID, string nickName, string mobilePhone, string weiXinName, string headPortrait, int userType, int status)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            db.Proc_UserInfo_UpdateUserInfo(userID, nickName, mobilePhone,weiXinName,headPortrait, userType,status);
            return true;
        }



        /// <summary>
        /// 获取用户信息 by UserID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static Proc_UserInfo_SelectUserInfoResult GetUserInfo(string userID)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            return db.Proc_UserInfo_SelectUserInfo(userID).FirstOrDefault();
        }


        /// <summary>
        /// 获取用户状态列表
        /// </summary>
        /// <returns></returns>
        public static List<Proc_UserStatus_SelectListResult> GetUserStatusList()
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            return db.Proc_UserStatus_SelectList().ToList();
        }


        /// <summary>
        /// 修改用户状态（启用  禁用）
        /// </summary>
        /// <returns></returns>
        public static bool ChangeStatus(string userID,int status)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            db.Proc_UserStatus_UpdateStatus(userID, status);
            return true;
        }


        /// <summary>
        /// 获取用户级别列表
        /// </summary>
        /// <returns></returns>
        public static List<Proc_UserType_SelectListResult> GetUserTypeList()
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            return db.Proc_UserType_SelectList().ToList();
        }


        /// <summary>
        /// 用户购买,赠送的课程
        /// </summary>
        /// <returns></returns>
        public static List<Proc_UserCourse_SelectListResult> GetUserCourse(string userID, int payType, int pageIndex, int pageSize, ref int? pageCount)
        {
          XZL_GMDataContext db = new XZL_GMDataContext();
          return db.Proc_UserCourse_SelectList(userID, payType, pageIndex, pageSize, ref pageCount).ToList();
        }



        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <param name="userType">1：前台用户   2：后台用户</param>
        /// <returns></returns>
        public static UserInfo UserLogin(string userName, string userPwd, int userType)
        {      
           XZL_GMDataContext db = new XZL_GMDataContext();
            var obj = db.Proc_UserInfo_Login(userName, userPwd, userType).FirstOrDefault();
            if (obj == null)
            {
                return null;
            }
            else
            {

                UserInfo user = new UserInfo();

                user.UserID = obj.UserID;
                user.UserType = Convert.ToString(obj.UserType);
                user.UserName = obj.UserName;
                user.NickName = obj.NickName;
                user.Email = obj.Email;
                user.Password = obj.Password;
                user.Status = obj.Status;              
  
                return user;
            }
        }


        /// <summary>
        /// 添加登录日志
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="logIP"></param>
        /// <returns></returns>
        public static bool AddUserLog(string userID, string logIP)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            db.Proc_UserLog_Add(userID, logIP);
            return true;
        }


        /// <summary>
        /// 获取所有菜单项
        /// </summary>
        /// <returns></returns>
        public static List<Proc_MenuManage_SelectAllResult> GetAllMenuList()
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            return db.Proc_MenuManage_SelectAll().ToList();
        }


        /// <summary>
        /// 获取子菜单列表
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public static List<Proc_MenuManage_SelectListByParentResult> GetMenuByParent(int parentID)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            return db.Proc_MenuManage_SelectListByParent(parentID).ToList();
        }

        /// <summary>
        /// 更新管理员权限
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="menuList"></param>
        /// <returns></returns>
        public static bool UpdateUserRole(string userID, string menuList)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            db.Proc_UserRole_Update(userID, menuList);
            return true;
        }



        /// <summary>
        /// 获取用户权限列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static Proc_UserRole_SelectResult GetUserRole(string userID)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            return db.Proc_UserRole_Select(userID).FirstOrDefault();
        }


        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="pwdOld"></param>
        /// <param name="pwdNew"></param>
        /// <param name="type">1:登录密码   2:二级密码</param>
        /// <returns></returns>
        public static int UpdateUserPwd(string userID,string pwdOld,string pwdNew)
        { 
        
         XZL_GMDataContext db = new XZL_GMDataContext();
         return db.Proc_UserInfo_BackUpdatePwd(userID, pwdOld, pwdNew);
        
        }


        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public static List<Proc_UserInfo_SelectLowerListResult> GetLowerUserList(string referrerID, int pageIndex, int pageSize, ref int? pageCount)
        {
            Request_GmDataContext rd=new Request_GmDataContext();
            return  rd.Proc_UserInfo_SelectLowerList(referrerID,pageIndex,pageSize,ref pageCount).ToList();
        }



        /// <summary>
        ///修改邀请人
        /// </summary>
        /// <returns></returns>
        public static bool UpdateReferrer(string userID, string mobilePhone)
       {
         XZL_GMDataContext db = new XZL_GMDataContext();
          db.Proc_UserInfo_UpdateReferrer(userID, mobilePhone);
          return true;
       }


        /// <summary>
        ///查询下级用户
        /// </summary>
        /// <returns></returns>
        public static List<Proc_UserInfo_LowerExpenseResult> GetLowerExpense(string userID, int pageIndex, int pageSize, ref int? pageCount)
        {
          XZL_GMDataContext db = new XZL_GMDataContext();
         return db.Proc_UserInfo_LowerExpense(userID, pageIndex, pageSize, ref pageCount).ToList();
        }

        
        /// <summary>
        ///查询根据手机号用户信息
        /// </summary>
        /// <returns></returns>      
        public static  Proc_UserInfo_SelectUserInfoByPhoneResult UserPhone(string mobilePhone)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
          return db.Proc_UserInfo_SelectUserInfoByPhone(mobilePhone).FirstOrDefault();
        }


        /// <summary>
        /// 编辑备注信息
        /// </summary>
        /// <returns></returns>   
        public static bool UpdateRealName(string userID,string realName)
        {
            XZL_GMDataContext db = new XZL_GMDataContext();
            db.Proc_UserInfo_UpdateRealName(userID,realName);
            return true;
        }
    }
}
